﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WorkflowRelevantData = ScenarioFramework.WorkflowEngine.DataModel.WorkflowRelevantData;

namespace ScenarioFramework.Tools.UI.Controls.ListControls
{
    public partial class AttributeList : ListView
    {
        WorkflowRelevantData.XMLAttributes xmlAttributes = null;

        public AttributeList()
        {
            InitializeComponent();

            this.BuildGridHeader();
        }

        private void BuildGridHeader()
        {
            this.Clear();

            this.Columns.Add("Name", 100, HorizontalAlignment.Left);
            this.Columns.Add("Value", 250, HorizontalAlignment.Left);
        }

        public void LoadProcessAttributes(long lProcessId)
        {
            try
            {
                xmlAttributes = WAPI.Process.GetAttributes(lProcessId);
            }
            catch (WAPI.Exception.WAPIException err)
            {
                MessageBox.Show(err.Message);
                return;
            }
            if ( xmlAttributes.Count == 0 )
            {
                //TODO:
                this.Enabled = false;
            }
            else
            {
                foreach ( WorkflowRelevantData.XMLAttribute xmlAttribute in xmlAttributes)
                {
                    //ListViewItem item = new ListViewItem(new string[] { xmlAttribute.Name, xmlAttribute.Value });

                    this.Items.Add(new ListItem.AttributeListViewItem(xmlAttribute));
                }

                this.Enabled = true;
            }
        }

        public WorkflowRelevantData.XMLAttribute AttributeSelected {
            get 
            {
                ListItem.AttributeListViewItem item = (ListItem.AttributeListViewItem)this.SelectedItems[0];
                return item.XMLAttribute;
            }
            set
            {
                ListItem.AttributeListViewItem item = (ListItem.AttributeListViewItem)this.SelectedItems[0];
                item.SubItems[0].Text = value.Name;
                item.SubItems[1].Text = value.Value;
                item.XMLAttribute = value;
            }
        }

        public WorkflowRelevantData.XMLAttributes XMLAttributes
        {
            get 
            {
                WorkflowRelevantData.XMLAttributes xmlAttributes = new WorkflowRelevantData.XMLAttributes();
                foreach (ListItem.AttributeListViewItem item in this.Items)
                {
                    xmlAttributes.Add(item.XMLAttribute);
                }
                return xmlAttributes;
            }
        }
    }
}
